
package cn.stylefeng.guns.modular.business.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.common.GunsData;
import cn.stylefeng.guns.modular.business.entity.MessageNotice;
import cn.stylefeng.guns.modular.business.model.result.IndexResult;
import cn.stylefeng.guns.modular.business.service.MessageNoticeService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/message")
public class MessageNoticeController extends BaseController {

    private String PREFIX = "/business/messageNotice/";

    @Autowired
    private MessageNoticeService messageNoticeService;


    @RequestMapping("")
    public String index() {
        return PREFIX + "message_notice.html";
    }


    @RequestMapping("/message_notice_add")
    public String noticeAdd() {
        return PREFIX + "message_notice_add.html";
    }


    @RequestMapping("/message_notice_update/{id}")
    public String noticeUpdate(@PathVariable String id, Model model) {
        MessageNotice notice = messageNoticeService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(notice));
        return PREFIX + "message_notice_edit.html";
    }


    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page page = LayuiPageFactory.defaultPage();
        List<MessageNotice> list = messageNoticeService.list(page, condition, null);
        list.forEach(messageNotice -> {
            if ("0".equals(messageNotice.getType())) {
                messageNotice.setType("公告");
            }
            if ("1".equals(messageNotice.getType())) {
                messageNotice.setType("系统消息");
            }
            if ("2".equals(messageNotice.getType())) {
                messageNotice.setType("平台通知");
            }
        });
        page.setRecords(list);
        return LayuiPageFactory.createPageInfo(page);
    }


    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MessageNotice notice) {
        if (ToolUtil.isOneEmpty(notice, notice.getTitle(), notice.getContent(), notice.getType())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        notice.setCreateUser(ShiroKit.getUserNotNull().getId());
        notice.setCreateTime(new Date());
        messageNoticeService.add(notice.getTitle(), notice.getContent(), notice.getType(), notice.getCreateUser());
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String messageId) {
        messageNoticeService.removeById(messageId);
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MessageNotice notice) {
        if (ToolUtil.isOneEmpty(notice, notice.getId(), notice.getTitle(), notice.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        MessageNotice old = messageNoticeService.getById(notice.getId());
        old.setTitle(notice.getTitle());
        old.setContent(notice.getContent());
        messageNoticeService.updateById(old);
        IndexResult indexResult = GunsData.indexMap.get(Constants.INDEX_MAP_KEY);
        if (indexResult.getMessageNoticeResult() != null) {
            indexResult.setMessageNoticeResult(null);
        }
        return SUCCESS_TIP;
    }

}
